<template>
  <div class="login">
    <el-form :model="form" status-icon :rules="rules" ref="loginForm" label-width="80px" class="login-box">
      <h3 class="login-title">欢迎登录</h3>
      <el-form-item label="账号" prop="username">
        <el-input type="text" placeholder="请输入账号" v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" placeholder="请输入密码" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('loginForm')" :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="chat('message')">ai</el-button>
    <el-link href="http://localhost:8080/about">注册</el-link>

    <p id="ww">{{fff}}</p>
  </div>

</template>
<script>
import axios from 'axios'; //调用接口插件
import Qs from 'qs'
import { MessageBox } from "element-ui";
export default {
  name: 'login',
  props: {
    msg: String
  },
  data () {
    return {
      fff: '',
      logining:false,
      message:'',
      form: {
        username: '',
        password: ''
      },
      // 表单验证，需要在 el-form-item 元素中增加 prop 属性
      rules: {
        username: [
          { required: true,message: '账号不可为空',trigger: 'blur' }
        ],
        password: [
          { required: true,message: '密码不可为空',trigger: 'blur' },
          {validator: (rule, value, callback) => {
            if (value.length < 6) {
              callback(new Error('密码不能小于6位'));
            } else
              callback();
          }
          }
        ]
      }

    };
  },
  computed: {
  },
  methods:{
    onSubmit(formName) {
      // 为表单绑定验证功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.logining = true,
              console.log(this.form.username)
          console.log(this.form.password)
          axios({
            method: 'post',
            url: 'http://localhost:8080/login',
            data: Qs.stringify({
              username: this.form.username,
              password: this.form.password
            }),
            header:"application/x-www-form-urlencoded"
          })

              .then(res => {//
            console.log(res);
            console.log(res.data);
                localStorage.setItem('token', res.data);
                const token = localStorage.getItem('token');
                console.log(token);
            if(res.data === '') {
              MessageBox.confirm('当前未登录', '提示', {
              }).then(() => {
                // 开始倒计时
                let countdown = 3;
                const timer = setInterval(() => {
                  countdown--;
                  if (countdown <= 0) {
                    clearInterval(timer);
                    this.$router.push("/register");
                  }
                }, 1000);
              })
            }
          })
              .catch(err => {
                console.log(err)
                console.log(err.code)
                if(err.code === 'ERR_BAD_REQUEST') {
                  this.$router.push("/test");
                }
                else
                  this.$router.push("/register");
              })

          this.logining = false;
          console.log('submit!');
          if(this.form.username == 'admin' && this.form.password == '123456') {
            this.$router.push("/HelloWorld");
          }
          // })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  chat(message) {
    const token = localStorage.getItem('token');
    axios({
      url:
          "http://localhost:8080/ai/chat",
      method: "post",
      headers:"application/x-www-form-urlencoded",
      data:(
          Qs.stringify({
              message:"中国最大城市",
              }
          ))
    })
        .then(res => {
          console.log(res.data)
          this.fff= res.data;
        })
        .catch(err => {
          console.log(err)
          this.$router.push("/register")
        })
  }
  }
}
</script>
<style scoped>
.login-box {
  border: 1px solid #DCDFE6;
  width: 350px;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399;
}
.login-title {
  text-align: center;
  margin: 0 auto 40px auto;
  color: #303133;
}

</style>
